import { CartItemModel } from "./declare"
import { observable, computed, action } from "mobx"
import { math } from "@/utils/math"
import { getUserId, launchPay } from '@/utils/utils';
import { network } from "@/utils/network"
import { DesktopOrderType } from '@/utils/constants';
import { cartModel } from "./CartModel"
import Taro from '@tarojs/taro'
import { sum as _sum } from 'lodash';
import { sceneDesktopManager } from "./SceneDesktopManger";


/**
 * 结算列表model
 */
class CheckListModel {

  people_number: Number;
  hooks: Array<(cart: CartItemModel) => void>

  @observable.ref
  items: CartItemModel[]

  table_number: string

  @observable
  id: number

  @observable.ref
  address: any

  @observable
  isCatering: boolean

  @observable.ref
  shopDetail: any

  @observable
  tablewareCost: number

  @observable
  packCost: number

  note: string

  constructor() {
    this.items = []
    this.hooks = []
    this.people_number = 0
    this.tablewareCost = 0
    this.packCost = 0
    this.table_number = '0'
    this.id = 0
    this.address = {}
    this.note = ''
  }

  @computed
  get total() {
    return this.items.reduce((p, n) => p + n.number, 0);
  }

  @computed
  get totalFree() {
    const goodFree = math.format(this.items.filter(t => t.Good).reduce((p, n) => {
      return p + n.Good.price * n.number
    }, 0) / 100);

    let free = (+goodFree);
    // 餐饮
    if (this.isCatering) {
      // 堂食
      if (sceneDesktopManager.orderType === DesktopOrderType[0]) {
        free += this.tablewareCost / 100;
      }
      // 外卖(打包费)
      if (sceneDesktopManager.orderType !== DesktopOrderType[0]) {
        free += this.packCost ? this.packCost / 100 : 0;
      }
    }
    return math.format(free);
  }

  @action
  setData(cartList) {
    this.items = cartList
    this.packCost = _sum(cartList.map(it => it.number || 1)) * cartModel.shopDetail.pack;
    this.items.forEach((it) => {
      this.hooks.length > 0 && this.hooks.forEach(hook => hook(it))
    })
  }

  @action
  setValue(key, value) {
    this[key] = value;
    if (key === 'people_number') {
      this.tablewareCost = value * cartModel.shopDetail.tableware;
      this.totalFree;
    }
  }


  getData() {
    network.get('/xxm/cart/list', {
      user_id: getUserId()
    }).then((rs) => {
      this.setData(rs.data.list)
    });
  }

  // 提交订单
  async orderPay() {

    // 餐饮才需要选择order_type
    const hasNext = await sceneDesktopManager.next();
    if (!hasNext) return;
    if (
      !this.people_number &&
      this.isCatering &&
      sceneDesktopManager.orderType === DesktopOrderType[0]
    ) {
      Taro.showToast({ icon: 'none', title: '就餐人数不能为空' });
      return;
    }




    const params = {
      user_id: getUserId(),
      note: this.note,
      order_type: sceneDesktopManager.orderType, // 堂食
      people_number: this.isCatering ? this.people_number : 0,
      table_number: this.isCatering ? sceneDesktopManager.getDesktopNo() : '0'
    };

    if (
      this.id === 0 &&
      ((
        sceneDesktopManager.orderType === DesktopOrderType[1] &&
        this.isCatering
      ) || (!this.isCatering))
    ) {
      Taro.showToast({ icon: 'none', title: '请选择收货地址' });
      return;
    }

    if (!this.isCatering ||
      (this.isCatering && sceneDesktopManager.orderType === DesktopOrderType[1])) {
      Object.assign(params, { address_id: this.id });
    }

    network.post('/xxm/order/pay', params).then(res => {
      const { data: { appid, prepay_id } } = res;
      this.items.forEach((it) => {
        this.hooks.length > 0 && this.hooks.forEach(hook => hook({ ...it, number: 0 }))
      })
      cartModel.clear()
      launchPay({ appid, prepay_id })

      console.log(res, '支付订单');
    })
  }
}

export const checkListModel = new CheckListModel()